package org.msfj.admin.org.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.admin.model.Menu;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.org.mapper.MenuMapper;
import org.msfj.admin.org.mapper.RoleMenuMapper;
import org.msfj.admin.org.service.MenuService;
import org.msfj.admin.vo.MenuVo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.msfj.common.enums.MenuType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统菜单信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "menu")
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    RedisTemplate redisTemplate;


    @Override
//    @CacheEvict(key = Constants.ALL_PERMS_KEY)
    public Menu saveOrEdit(Menu entity, Class<?>... cls) {
        Menu menu = super.saveOrEdit(entity, cls);
        redisTemplate.delete(Constants.ALL_PERMS_KEY);
        return menu;
    }

    /**
     * 根据条件分页查询系统菜单信息列表
     *
     * @param menuVo
     * @param page   当前第几页
     * @param rows   每页多少条记录
     * @return PageInfo<Menu>
     */
    @Override
    //@Cacheable
    public PageInfo<MenuVo> getByMenu(MenuVo menuVo, int page, int rows) {
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (menuVo != null) {
            if (menuVo.getParentId() != null) {
                criteria.andEqualTo("parentId", menuVo.getParentId());
            }
        }
        PageHelper.startPage(page, rows);
        List<Menu> menuList = super.getByExample(example);
        PageInfo menuVoPageInfo = new PageInfo(menuList);
        menuVoPageInfo.setList(setOtherProperties(menuList));
        return menuVoPageInfo;
    }

    /**
     * 根据条件查询所有系统菜单信息对象列表
     *
     * @param menuVo
     * @return List<Menu>
     */
    @Override
    //@Cacheable
    public List<MenuVo> getAllMenu(MenuVo menuVo) {
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        example.orderBy("sortNo").asc();
        List<MenuVo> menuVoList = setOtherProperties(super.getByExample(example));
        return menuVoList;
    }

    /**
     * 根据条件查询系统菜单信息的tree对象
     *
     * @return
     */
    @Override
    //@Cacheable
    public List<MenuVo> getMenuTree(boolean onlyMenu) {
        List<MenuVo> menuVos = this.getAllMenu(null);
        List<MenuVo> rootMenus = CollectionUtil.newArrayList();
        if (CollectionUtil.isNotEmpty(menuVos)) {
            for (MenuVo menuVo : menuVos) {
                menuVo.setLabel(menuVo.getName());
                if(StrUtil.isNotBlank(menuVo.getTreePosition())){
                    String[] split = menuVo.getTreePosition().split("&");
                    menuVo.setLevel(split.length);
                }else{
                    menuVo.setLevel(1);
                }
                if (ObjectUtil.equal(menuVo.getParentId(), "0")) {
                    rootMenus.add(menuVo);
                }
                for (MenuVo childVo : menuVos) {
                    boolean allMenu = true;
                    if (onlyMenu) {
                        allMenu = childVo.getType().equals(MenuType.MENU.getCode());
                    }
                    if (ObjectUtil.equal(childVo.getParentId(), menuVo.getId()) && allMenu) {
                        if (ObjectUtil.isNull(menuVo.getChildren())) {
                            List<MenuVo> children = CollectionUtil.newArrayList();
                            children.add(childVo);
                            menuVo.setChildren(children);
                        } else {
                            menuVo.getChildren().add(childVo);
                        }
                    }
                }
            }
        }
        return rootMenus;
    }

    /**
     * 根据用户id获取用户的所有菜单
     *
     * @param userId
     * @return
     */
    @Override
    //@Cacheable
    public List<Menu> getAllMenuByUserId(String userId) {
        return menuMapper.selectAllMenuByUserId(userId);
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param menu
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public MenuVo setOtherProperties(Menu menu) {
        MenuVo menuVo = new MenuVo();
        BeanUtils.copyProperties(menu, menuVo);
        return menuVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param menus
     * @return List<MenuVo>
     */
    @Override
    //@Cacheable
    public List<MenuVo> setOtherProperties(List<Menu> menus) {
        List<MenuVo> menuVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(menus)) {
            for (Menu menu : menus) {
                MenuVo menuVo = new MenuVo();
                BeanUtils.copyProperties(menu, menuVo);
                menuVos.add(menuVo);
            }
        }
        return menuVos;
    }

    @Override
//    @CacheEvict(key = Constants.ALL_PERMS_KEY)
    public void delById(MenuVo menuVo) {
        Example example = new Example(RoleMenu.class);
        example.createCriteria().andEqualTo("menuId", menuVo.getId());
        roleMenuMapper.deleteByExample(example);
        super.delById(menuVo);
        redisTemplate.delete(Constants.ALL_PERMS_KEY);

    }
}
